using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ImageAcquisitionTAL;
using HomeDms.Properties;
using System.Threading;
using HomeDmsUtils;

namespace HomeDms
{
    public partial class ScanForm : Form
    {
        public ScanForm()
        {
            Cursor = Cursors.WaitCursor;
            InitializeComponent();
        }

        public event EventHandler GotImage;

        public Image ScannedImage
        {
            get
            {
                return pictureBox3.OriginalImage;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            AcquireImage(true, false);
        }

        private void DoGotImage()
        {
            if (GotImage != null)
                GotImage(this, EventArgs.Empty);
        }

        private void scanallFromFeederToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AcquireImage(false, false);
        }

        private void AcquireImage(bool fromGlass, bool forPreview)
        {
            Image img = null;
            stopScanClicked = false;
            ScannerInfo sinfo = scannersCombobox1.SelectedScannerInfo;
            sinfo.CurrentResolution = resolutionsComboBox1.SelectedResolution;
            sinfo.CurrentColorMode = colorsComboBox1.SelectedColorMode;
            sinfo.CurrentSource = fromGlass | forPreview ? ScanSource.Flatbed : ScanSource.Feeder;
            scanningSession = scannersCombobox1.SelectedDeviceTechnology.BeginAcquire(sinfo);
            do
            {
                try
                {
                    if (forPreview)
                        img = scannersCombobox1.SelectedDeviceTechnology.AcquireForPreview(sinfo);
                    else
                        img = scannersCombobox1.SelectedDeviceTechnology.Acquire(sinfo);
                }
                catch (Exception ex)
                {
                    Localizer.MsgBox(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
                if (img != null)
                {
                    pictureBox3.OriginalImage = img;
                    if (!forPreview)
                        DoGotImage();
                }
                Application.DoEvents();
            } while (img != null && !fromGlass && !forPreview && !stopScanClicked); // if img = null -> no more pages to scan
            scannersCombobox1.SelectedDeviceTechnology.EndAcquire(scanningSession);
        }

        object scanningSession = null;

        private void scannersCombobox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool IsDevice = scannersCombobox1.SelectedDeviceID != null;
            previewToolStripButton.Enabled = IsDevice;
            StartToolStripButton.Enabled = IsDevice;
            StopToolStripButton1.Enabled = IsDevice;
            if (IsDevice)
            {
                ScannerLineGuiHelper line = (ScannerLineGuiHelper)scannersCombobox1.SelectedItem;
                ScannerInfo scanner = line.Technology.GetScannerInfo(line.DeviceID);
                scanallFromFeederToolStripMenuItem.Enabled = scanner.SupportsFeeder;
                StopToolStripButton1.Enabled = scanner.SupportsFeeder;
                resolutionsComboBox1.Resolutions = scanner.SupportedResolutions;
                resolutionsComboBox1.SelectedResolution = scanner.CurrentResolution;
                colorsComboBox1.SetColorsFromScannerInfo(scanner);
                colorsComboBox1.SelectedColorMode = scanner.CurrentColorMode;
            }
            this.Activate(); // this is because of some ugly splash screens the datasource can present on start
        }

        private void previewToolStripButton_Click(object sender, EventArgs e)
        {
            AcquireImage(true, true);
        }

        private void AdvancedtoolStripButton_Click(object sender, EventArgs e)
        {
            if (AdvancedtoolStripButton.Checked)
                ExplodePanel();
            else
                ImplodePanel();
        }

        private void ImplodePanel()
        {
            for (int i = 1; i <= 170; i++)
            {
                this.Height = 710 - i;
                Application.DoEvents();
            }
        }

        private void ExplodePanel()
        {
            for (int i = 1; i <= 170; i++)
            {
                this.Height = 710 - 170 + i;
                Application.DoEvents();
            }
        }

        private void ScanForm_Load(object sender, EventArgs e)
        {                        
            scannersCombobox1.SelectedDeviceID = Settings.Default.DefaultScanner;
            Cursor = Cursors.Default;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Settings.Default.DefaultScanner = scannersCombobox1.SelectedDeviceID;
            Settings.Default.DefaultColorMode = colorsComboBox1.SelectedColorMode;
            Settings.Default.DefaultResolution = resolutionsComboBox1.SelectedResolution;
            Settings.Default.Save();
        }

        bool stopScanClicked = false;

        private void StopToolStripButton1_Click(object sender, EventArgs e)
        {
            stopScanClicked = true;
        }
    }
}